#include "stdafx.h"
#include "ado/Updates.h"
#include "ado/DbMysql.h"
#include "standard/nstring.h"
//#include "ado/fun_sets.h"
Updates *Updates::_init = NULL;

Updates* Updates::init(void *db)
{
    _init = new Updates(db);
    return _init;
}



Updates::Updates(void *db)
{
    m_db = (tDataBase*) db; 
    std::string classname = m_db->classname;
    
    if (!(m_db->database).empty()) {
        m_dbname = m_db->database;
    }
    
    this->m_classname = tolower_str(classname);
	//GetFunInstance("tolower_str",this->m_classname, classname );
}


Updates::~Updates()
{
    m_db->database.clear();
    m_db->classname.clear();
}

Updates *Updates::setp(std::map<std::string, std::string> setstr)
{
    this->m_setstr      = setstr;
    this->setparam(this->m_setstr, this->m_classname);        
    return this;
}

int Updates::update(const char *tokens, ...)
{
   
    va_list vp; 
    va_start(vp, tokens);
    std::string wheres;
    
    std::string que;
    std::string getparam;
    
    this->m_classname = tolower_str(this->m_classname);
    	//GetFunInstance("tolower_str",this->m_classname,  this->m_classname );
    this->findByArray(tokens, vp);
    
    this->setparam(this->m_setstr, this->m_classname);
    getparam = this->getSet(); 
    
    que = "UPDATE " + m_dbname + "."  + m_prefix + this->m_classname 
            + " SET " + getparam
            + " WHERE " + this->getWhere();
    
    DbMysql::instance()->execute(que.c_str());            
    
    va_end(vp);        
        
    return true;
}
